﻿using Janus.BaseEntitys;
using Janus.Mes.DTO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Janus.Mes.Services.Interface
{
    /// <summary>
    /// 服务基础类
    /// </summary>
    /// <typeparam name="T">DTO数据传输对象</typeparam>
    [ServiceContract(Namespace = "http://mes.januscn.com/")]
    public interface IBase<T>
        where T : BaseDTO, new()
    {
        #region 是否存在查询
        /// <summary>
        /// 指定条件记录是否存在
        /// </summary>
        /// <param name="predicate">lamda条件</param>
        /// <returns></returns>
        [OperationContract]
        bool IsExists(string predicate);// where TEntity : BaseDTO;

        // <summary>
        /// 指定条件记录是否存在
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        [OperationContract]
        bool IsExists(Querier predicate);
        #endregion

        #region 查询
        /// <summary>
        /// 根据指定条件查询并返回指定字段
        /// </summary>
        /// <param name="field">返回字段</param>
        /// <param name="predicate">指定条件</param>
        /// <returns></returns>
        [OperationContract]
        object FindScalar(string field, string predicate);


        /// <summary>
        /// 根据指定条件查询并返回指定字段
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="field">返回字段</param>
        /// <param name="predicate">指定条件</param>
        /// <returns></returns>
        [OperationContract]
        object FindScalar(string field, Querier predicate);

        /// <summary>
        /// 根据主键获取单个对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [OperationContract]
        T FindOne(params object[] entity);

        /// <summary>
        /// 根据条件获取一个对象
        /// </summary>
        /// <param name="predicate">sql语句</param>
        /// <returns></returns>
        [OperationContract]
        T FindOne(string predicate);

        /// <summary>
        /// 根据条件获取一个对象
        /// </summary>
        /// <param name="predicate">lamda条件表达式</param>
        /// <returns></returns>
        [OperationContract]
        T FindOne(Querier predicate);

        /// <summary>
        /// 根据条件获取列表数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="predicate">lamda条件表达式</param>
        /// <param name="orderBy">排序</param>
        /// <returns></returns>
        [OperationContract]
        IList<T> FindAll(string predicate, string orderBy = null);
        
        /// <summary>
        /// 根据条件获取列表数据
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="predicate">lamda条件表达式</param>
        /// <param name="orderBy">排序</param>
        /// <returns></returns>
        [OperationContract]
        IList<T> FindAll(Querier predicate);

        /// <summary>
        /// 返回数据列表
        /// </summary>
        /// <param name="searchEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        [OperationContract]
        ResponseResult<IList<T>> FindAll(SearchEntity<T> searchEntity);

        /// <summary>
        /// 使用存储过程分页 性能最好
        /// </summary>
        /// <param name="searchEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        [OperationContract]
        ResponseResult<IList<T>> FindAllByPro(PageCriteria pageCriteria);

        #endregion

        #region 新增
        /// <summary>
        /// 添加一条记录
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [OperationContract]
        bool Add(T t);

        /// <summary>
        /// 添加多条记录，返回行数
        /// </summary>
        /// <param name="ts"></param>
        /// <returns></returns>
        [OperationContract]
        bool Add(List<T> ts);

        #endregion

        #region 更新
        /// <summary>
        /// 更新对象（根据主键）,不存在则新增
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [OperationContract]
        int Save(T t);

        /// <summary>
        /// 批量更新对象
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        [OperationContract]
        bool Save(List<T> entitys);

        /// <summary>
        /// 根据主键编辑对象
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [OperationContract]
        int Update(T t);

        /// <summary>
        /// 根据主健更新列表
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        [OperationContract]
        bool Update(List<T> entitys);

        /// <summary>
        /// 更新部分类
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>
        //int UpdateSetion(DynamicEntity entity, Conditioner conditions);

        /// <summary>
        /// 更新部分类
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>        
        [OperationContract]
        int UpdateSetion(T entity, Conditioner conditions);

        /// <summary>
        /// 更新部分类
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conditions"></param>
        /// <returns></returns>        
        [OperationContract]
        int UpdateSetion(T entity, Querier conditions);

        /// <summary>
        /// 更新部份类
        /// </summary>
        /// <param name="updater"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        [OperationContract]
        int UpdateSetion(Hashtable updater, string predicate);//where TEntity : Entity;

        /// <summary>
        /// 更新部份类
        /// </summary>
        /// <param name="updater"></param>
        /// <param name="predicate"></param>
        /// <returns></returns>
        [OperationContract]
        int UpdateSetion(Hashtable updater, Querier predicate);

        /// <summary>
        /// 更新停用启用状态
        /// </summary>
        /// <typeparam name="POCO"></typeparam>
        /// <param name="queriers"></param>
        /// <returns></returns>
        [OperationContract]
        bool UpdateIsEnable(Querier queriers);
        #endregion

        #region 删除
        /// <summary>
        /// 根据主键删除对象
        /// </summary>
        /// <param name="keyVal">主健值</param>
        /// <returns></returns>
        [OperationContract]
        bool Delete(params object[] entity);

        /// <summary>
        /// 根据复杂条件删除数据
        /// </summary>
        /// <param name="conditions"></param>
        /// <returns></returns>
        [OperationContract]
        bool Delete(Conditioner conditions);

        /// <summary>
        /// 根据复杂条件删除数据
        /// </summary>
        /// <param name="predicate">lamda条件</param>
        /// <returns></returns>
        [OperationContract]
        bool Delete(string predicate);// where TEntity : BaseDTO;
        
        /// <summary>
        /// 根据复杂条件删除数据
        /// </summary>
        /// <param name="predicate">lamda条件</param>
        /// <returns></returns>
        [OperationContract]
        bool Delete(Querier predicate);
        /// <summary>
        /// 清空表
        /// </summary>
        /// <returns></returns>
        //int DeleteAll();

        #endregion

        #region 批量操作
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        /// <returns></returns>
        bool BatchInset(List<T> models);

        /// <summary>
        /// 批量更新(需提供RowState状态)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        /// <returns></returns>
        bool BatchUpdate(List<T> models);

        /// <summary>
        /// 批量更新或插入(需提供RowState状态)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="models"></param>
        /// <returns></returns>
        bool BatchSave(List<T> models);
        #endregion
    }
}
